package com.cbg.sys.controller;

import com.cbg.base.constant.Constant;
import com.cbg.base.utils.Result;
import com.cbg.sys.entity.Route;
import com.cbg.sys.service.IRouteService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.cbg.base.core.SuperController;

import java.util.Collections;
import java.util.Optional;

/**
 * <p>
 * 路线表 前端控制器
 * </p>
 *
 * @author chenbing
 * @since 2018-12-13
 */
@RestController
@RequestMapping("/sys/route")
@Api(value = "路线控制器",tags = "路线操作接口")
public class RouteController extends SuperController<IRouteService, Route> {

    @Autowired
    RedisTemplate<String, Route> routeRedisTemplate;

    @ApiOperation(value="返回全部路线", notes="前端根据路线列表计算用户选择路线")
    @GetMapping("/all")
    public Result<?> routes(){
        return Result.ok(Optional.ofNullable(routeRedisTemplate.opsForList().range(Constant.ROUTE_KEY, Constant.LIST_START, Constant.LIST_END))
                .orElseGet(() -> Collections.singletonList(new Route().setOrigin("暂无路线，敬请期待!").setDestination("暂无路线，敬请期待!"))));
    }
}
